<?php
class FunduserDao {
	private $table = 'fund_user';
	public function searchList($where,$order='',$limit=0){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->dataList1($this->table,'*',$where,$order,$limit);
	}
	public function searchList1($where,$order='',$limit=0){
		$where = $this->searchWhere1($where);
		return ClassManager::getSql()->dataList1($this->table,'*',$where,$order,$limit);
	}
	
	public function getDataPairList($strs_list){
		$strs = implode(',', $strs_list);
		return ClassManager::getSql()->dataPairList($this->table,'id,serial_number,name,skype_id,qq,phone,job,employee_id,email '," where id in (".$strs.")");
	}
		
	public function searchNumber($where){
		$where = $this->searchWhere($where);
		$tmp = ClassManager::getSql()->onedata1($this->table,"count(*)  ",$where);
		return $tmp[0];
	}
	
	public function update($data,$id){
		ClassManager::getSql()->updates($this->table,$data,"where id=".intval($id));
	}
	public function updateInId($data,$id){
		$tmp  = implode(',',$id);
		ClassManager::getSql()->updates($this->table,$data,'where id in('.$tmp.')');
	}
	public function updateByEmail($data,$email){
		ClassManager::getSql()->updates($this->table,$data,"where email='".$email."'");
	}
	public function updateByPhone($data,$phone){
		ClassManager::getSql()->updates($this->table,$data,"where phone='".$phone."'");
	}
	public function get($id){
		return ClassManager::getSql()->oneDataCache($this->table, 'id',intval($id));
	}
	public function add($data){
		ClassManager::getSql()->insert($this->table,$data);
	}
	public function getByEmail($email){
		return  ClassManager::getSql()->oneDataCache($this->table,'email',$email);
	}
	public function getByPhone($phone){
		return ClassManager::getSql()->oneDataCache($this->table,'phone',$phone);
	}
	public function getByInviteUserId($user_id){
		return ClassManager::getSql()->oneDataCache($this->table,'user_id',intval($user_id));
	}
	public function deleteBatchByid($id){
		ClassManager::getSql()->delete($this->table,"where id in(".$id.")");
	}


	public function searchRecomendListForsumsale($where = array()){
		$where = $this->searchMutilWhere($where);
		$sql = "select sum(user_money)+sum(online_money) sumamount  ";
		$sql.= " from eo_order a left join eo_user b on a.user_id=b.id ";
		$sql.= " left join  eo_fund_user c on  b.fund_user_id=c.id ";
		$sql.= " where  1=1";
		$sql.= $where;
		$sql.= " group by c.fund_user_id ";
		$sumOrder = ClassManager::getSql()->dataMutillist(' sumorder and sumorder by funduser ',$sql);
		return $sumOrder[0]['sumamount'];
	}
	
	
	public function searchNumberForsumsale($where=array()){
		$where = $this->searchMutilWhere($where);
		$sql = " select count(1) as cnt from (select 1 ";
		$sql.= " from eo_order a left join eo_user b on a.user_id=b.id ";
		$sql.= " left join  eo_fund_user c on  b.fund_user_id=c.id ";
		$sql .= " where  1=1";
		$sql.= $where;
		$sql.= " group by b.fund_user_id) s ";
		$tmp =  ClassManager::getSql()->dataMutillist(' order and sumorder number by funduser ',$sql);
		return $tmp[0]['cnt'];
	}
			
	
	public function searchListForsumsale($where){
		$where = $this->searchMutilWhere($where);
		$sql = "select sum(user_money)+sum(online_money) sumamount,b.fund_user_id,c.name,c.fund_user_rank,c.serial_number  ";
		$sql.= " from eo_order a left join eo_user b on a.user_id=b.id ";
		$sql.= " left join  eo_fund_user c on  b.fund_user_id=c.id ";
		$sql.= " where  1=1";
		$sql.= $where;
		$sql.= " group by b.fund_user_id ";
		$sumOrder = ClassManager::getSql()->dataMutillist(' order and sumorder by funduser ',$sql);
		return $sumOrder;
	}
	
	
	public function searchNumberFordetailsale($where=array()){
		$where = $this->searchMutilWhere($where);
		$sql = " select count(1) as cnt from (select 1 ";
		$sql .= " from eo_order a left join eo_user b on a.user_id=b.id ";
		$sql .= " where  1=1";
		$sql.= $where;
		$sql.= " group by a.id) s ";
		$tmp =  ClassManager::getSql()->dataMutillist('  order and detailsale number by funduser ',$sql,$limit);
		return $tmp[0]['cnt'];
	}
			
	
	public function searchListFordetailsale($where){
		$where = $this->searchMutilWhere($where);
		$sql = "select sum(user_money)+sum(online_money) sumamount,a.order_no,b.english_name,b.email,b.phone,b.user_id,b.id,a.add_time ";
		$sql.= " from eo_order a left join eo_user b on a.user_id=b.id ";
		$sql.= " where  1=1";
		$sql.= $where;
		$sql.= " group by a.id ";
		
		$sumOrder = ClassManager::getSql()->dataMutillist(' order and detailsale by funduser ',$sql);
		return $sumOrder;
	}
	

	public function searchNumberForRecommenddetailsale($where=array()){
		$where = $this->searchMutilWhere($where);
		$sql = " select count(1) as cnt from (select 1 ";
		$sql.= " from eo_order a left join eo_user b on a.user_id=b.id ";
		$sql.= " left join  eo_fund_user c on  b.fund_user_id=c.id ";
		$sql .= " where  1=1";
		$sql.= $where;
		$sql.= " group by c.fund_user_id) s ";
		$tmp =  ClassManager::getSql()->dataMutillist(' order and sumorder number by funduser ',$sql);
		return $tmp[0]['cnt'];
	}
		
	
	public function searchListForRecommenddetailsale($where){
		$where = $this->searchMutilWhere($where);
		$sql = "select sum(user_money)+sum(online_money) sumamount,c.id as fund_user_id  ";
		$sql.= " from eo_order a left join eo_user b on a.user_id=b.id ";
		$sql.= " left join  eo_fund_user c on  b.fund_user_id=c.id ";
		$sql.= " where  1=1";
		$sql.= $where;
		$sql.= " group by c.fund_user_id ";
		$sumOrder = ClassManager::getSql()->dataMutillist(' order and sumorder by funduser ',$sql);
		return $sumOrder;
	}	
	
	public function searchMutilWhere($where){
		$sql="";
	
		if(isset($where['order_status_list'])&&!empty($where['order_status_list'])){
			$sql.=" a.order_status in (".ORDER_STATUS_0.",".ORDER_STATUS_1.",".ORDER_STATUS_2.")";
		}
	
		if(isset($where['pay_status'])&&!empty($where['pay_status'])){
			$sql.=" and a.pay_status = ".$where['pay_status'];
		}
	
		if(isset($where['start_time'])&&!empty($where['start_time'])){
			$where['start_time']= strtotime($where['start_time']);
			$sql.=" and a.add_time >= ".$where['start_time']."";
		}
	
		if(isset($where['end_time'])&&!empty($where['end_time'])){
			$where['end_time'] =  strtotime($where['end_time'])+ 86399;
			$sql.=" and a.add_time <= ".$where['end_time'];
		}
	
		if(isset($where['fund_user_id'])&&!empty($where['fund_user_id'])){
			$sql.=" and b.fund_user_id = ".$where['fund_user_id'];
		}
	
		if(isset($where['fund_user_flg'])&&!empty($where['fund_user_flg'])){
			$sql.= " and b.fund_user_id>0 ";
			if(isset($where['keyword'])&&!empty($where['keyword'])){
				$sql.=" and (c.name like '%".$where['keyword']."%'";
				$sql.=" or c.serial_number like '%".$where['keyword']."%'";
				$sql.=" or c.phone like '%".$where['keyword']."%')";
			}
			
			if(isset($where['recommend_user_id'])&&$where['recommend_user_id']>0){
				$sql.=" and c.fund_user_id = ".$where['recommend_user_id'];
			}
			
		}
		
	
		if(isset($where['fund_user_detail_flg'])&&!empty($where['fund_user_detail_flg'])){
			$sql.= " and b.fund_user_id>0 ";
			if(isset($where['keyword'])&&!empty($where['keyword'])){
				$sql.=" and (b.english_name like '%".$where['keyword']."%'";
				$sql.=" or b.email like '%".$where['keyword']."%'";
				$sql.=" or b.phone like '%".$where['keyword']."%')";
			}	
			
			if(isset($where['recommend_user_id'])&&$where['recommend_user_id']>0){
				$sql.=" and c.fund_user_id = ".$where['recommend_user_id'];
			}					
		}
		
		return $sql;
	}
	
	public function searchWhere($where){
		$sql = " where 1=1 ";
		if (!empty($where['start_time'])){
			$sql.=" and add_time >=".strtotime($where['start_time']);
		}
		if (!empty($where['end_time'])){
			$end_time  = strtotime($where['end_time'])+86399;
			$sql.=" and add_time <=".$end_time;
		}
		if(isset($where['status'])&&$where['status']!==""){
			$sql.=" and status =".$where['status'];
		}
	
		if(isset($where['fund_user_rank'])&&$where['fund_user_rank']>0){
			$sql.=" and fund_user_rank ='".$where['fund_user_rank']."'";
		}
	
		if(isset($where['phone'])&&$where['phone']>0){
			$sql.=" and phone ='".$where['phone']."'";
		}
		
		if(isset($where['fund_user_id'])&&$where['fund_user_id']>0){
			$sql.=" and fund_user_id ='".$where['fund_user_id']."'";
		}
	
		if(isset($where['id_list'])&&$where['id_list']){
			$sql.=" and id in (".$where['id_list_str'].")";
		}
		
		if(isset($where['key_word'])&&$where['key_word']!==""){
			$sql.=" and (name like '%".$where['key_word']."%'";
			$sql.=" or phone like '%".$where['key_word']."%'";
			$sql.=" or email like '%".$where['key_word']."%'";
			$sql.=" )";
		}
	
		return $sql;
	}
}

?>